/**
 * Created by xihashao-pc on 2016/7/12.
 */
'use strict';
angular.module('myApp',['ionic','myApp.config'])
    .controller("giftCtrl",function($scope,$http, $q,ENV,$ionicModal) {

        $scope.order = {number: 1, giftId: null};
        // $scope.giftList=[{title:"【花样年华】卡哇伊1",pic:"lib/images/head_01.jpg",price:190,id:1},
        //     {title:"【花样年华】卡哇伊2",pic:"lib/images/head_01.jpg",price:190,id:2},
        //     {title:"【花样年华】卡哇伊3",pic:"lib/images/head_01.jpg",price:190,id:3},
        //     {title:"【花样年华】卡哇伊4",pic:"lib/images/head_01.jpg",price:190,id:4},
        //     {title:"【花样年华】卡哇伊5",pic:"lib/images/head_01.jpg",price:190,id:5},
        //     {title:"【花样年华】卡哇伊5",pic:"lib/images/head_01.jpg",price:190,id:6}];
        $scope.giftList = {};
        //获取礼品列表


        $scope.getGiftList = function () {
            var serveurl = ENV.api + '/gift_list.json';
            $http({
                method: 'GET',
                url: serveurl,
                params: {}
            }).then(function (response) {
                console.log('giftList response', response);
                var data = response.data;
                if (data.success) {
                    $scope.giftList = data.giftList;
                } else {

                }

            }, function (err) {

            });

        };

        $scope.getGiftList();
        //弹出订单modal
        $ionicModal.fromTemplateUrl('giftOrder.html', {
            scope: $scope,
            animation: 'slide-in-up'
        }).then(function (modal) {
            $scope.modal = modal;
        })

        $scope.openModal = function (id, name, description) {
            $scope.order.giftId = id;
            $scope.order.giftName = name;
            $scope.order.giftDescrition = description;

            alert($scope.order.giftId);
            $scope.modal.show();
        }
        $scope.closeModal = function () {
            $scope.modal.hide();
        };
        //当我们用到模型时，清除它！
        $scope.$on('$destroy', function () {
            $scope.modal.remove();
        });
        // 当隐藏的模型时执行动作
        $scope.$on('modal.hide', function () {
            // 执行动作
        });
        // 当移动模型时执行动作
        $scope.$on('modal.removed', function () {
            // 执行动作
        });


        //订单数量+ -
        $scope.addNum = function () {
            $scope.order.number++;
        };
        $scope.subNum = function () {
            if ($scope.order.number > 1) {
                $scope.order.number--;
            }

        };
        //提交兑换礼品订单
        var submitOrder = function () {
            var d = $q.defer();
            var promise = d.promise;
            var serveurl = ENV.api + '/gift_buy.json';
            $http({
                method: 'GET',
                url: serveurl,
                params: {
                    id: $scope.order.giftId,
                    amount: $scope.order.number
                }
            }).then(function (response) {
                console.log('getUserInfo response', response);

                if (response.data) {
                    d.resolve(response.data);
                } else {
                    d.reject('获取礼品列表失败');
                }


            }, function (err) {
                d.reject('网络错误');
            });
            return promise;
        };
        $scope.submitOrder = function () {
            var serveurl = ENV.api + '/gift_buy.json';
            $http({
                method: 'GET',
                url: serveurl,
                params: {
                    id: $scope.order.giftId,
                    amount: $scope.order.number
                }
            }).then(function (response) {
                console.log(' response', response);

                if (response.data.success==true) {
                    alert("礼品兑换成功！");
                } else {
                	alert("礼品兑换失败！");
                }


            }, function (err) {
            	alert(err.data.msg);
            });

        };
    })
    